﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace ClassLibrary
{
    public class ConfigUtil
    {

        #region 【App.Config】
        #region 1.读取的配置信息项
        public static string GetAppSetting(string Key)
        {
            return ConfigurationManager.AppSettings[Key];
        }
        public static string GetConnectionString(string Name)
        {
            return ConfigurationManager.ConnectionStrings[Name].ConnectionString;
        }
        #endregion 读取的配置信息项

        #region 2.添加的配置信息项
        public static void AddAppSetting(string Key, string Value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings.Add(Key, Value);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
        public static void AddConnectionString(string Name, string connectionString)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ConnectionStringSettings css = new ConnectionStringSettings(Name, connectionString, "System.Data.SqlClient");
            config.ConnectionStrings.ConnectionStrings.Add(css);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("connectionStrings");
        }
        #endregion 添加的配置信息项

        #region 3.修改的配置信息项
        public static void SetAppSetting(string Key, string Value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings[Key].Value = Value;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
        public static void SetConnectionString(string Name, string connectionString)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.ConnectionStrings.ConnectionStrings[Name].ConnectionString = connectionString;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("connectionStrings");
        }
        #endregion 修改的配置信息项

        #region 4.删除配置信息项
        public static void DelAppSetting(string Key)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings.Remove(Key);
            config.Save(ConfigurationSaveMode.Modified);
        }
        public static void DelConnectionString(string Name)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.ConnectionStrings.ConnectionStrings.Remove(Name);
            config.Save(ConfigurationSaveMode.Modified);
        }
        #endregion 删除的配置信息项
        #endregion 【App.Config】

        public static string GetAppSettingsValue(string key)
        {
            ConfigurationManager.RefreshSection("appSettings");
            return ConfigurationManager.AppSettings[key] ?? string.Empty;
        }
        public static bool UpdateAppSettings(string key, string value)
        {
            var _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (!_config.HasFile)
            {
                throw new ArgumentException("程序配置文件缺失！");
            }
            KeyValueConfigurationElement _key = _config.AppSettings.Settings[key];
            if (_key == null)
                _config.AppSettings.Settings.Add(key, value);
            else
                _config.AppSettings.Settings[key].Value = value;
            _config.Save(ConfigurationSaveMode.Modified);
            return true;
        }

        public static string GetConnectionStringsValue(string name)
        {
            ConfigurationManager.RefreshSection("connectionStrings");
            return ConfigurationManager.ConnectionStrings[name].ConnectionString ?? string.Empty;
        }

        public static bool UpdateConnectionStrings(string configName, string name, string connectionString)
        {
            return UpdateConnectionStrings(configName, name,connectionString, null);
        }

        public static bool UpdateConnectionStrings(string configName, string name, string connectionString, string providerName)
        {
            bool result = false;

            try
            {
                string fileName = System.AppDomain.CurrentDomain.BaseDirectory + configName;// "App.config";
                XmlDocument doc = new XmlDocument();
                doc.Load(fileName);
                //
                XmlNode node = doc.SelectSingleNode("//connectionStrings");
                //
                XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
                if (element != null)
                {
                    //存在则更新子节点
                    element.SetAttribute("connectionString", connectionString);
                    if (!string.IsNullOrWhiteSpace(providerName))
                    {
                        element.SetAttribute("providerName", providerName);
                    }
                }
                else
                {
                    //不存在则新增子节点
                    XmlElement subElement = doc.CreateElement("add");
                    subElement.SetAttribute("name", name);
                    subElement.SetAttribute("connectionString", connectionString);
                    if (!string.IsNullOrWhiteSpace(providerName))
                    {
                        subElement.SetAttribute("providerName", providerName);
                    }
                    node.AppendChild(subElement);
                }

                //保存至配置文件(方式二)
                doc.Save(fileName);
                return true;
            }
            catch (Exception ex)
            {
                
                throw;
            }

            return result;
        }
    }
}
